<template>
  <div>
    <el-card class="box-card margin_30">
      <el-form :model="userInfo" label-width="80px" :inline="true" size="mini">
        <el-row :gutter="20">
          <el-col :span="6" :offset="0">
            <el-form-item label="昵称">
              <el-input v-model="userInfo.nickName" placeholder="昵称模糊搜索" />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="用户名">
              <el-input v-model="userInfo.username" placeholder="用户名模糊搜索" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="6" :offset="18">
            <el-button size="mini">重置</el-button>
            <el-button size="mini" type="primary">搜索</el-button>
          </el-col>
        </el-row>

      </el-form>
    </el-card>

    <el-card shadow="always" class="box-card margin_30">
      <div slot="header">
        <span>
          <el-button type="primary" size="mini" @click="isShow = true,isAdd = true">新增</el-button>
        </span>
      </div>
      <div>
        <el-table
          :data="userList"
          border
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="#"
            fixed="left"
            width="50"
            align="center"
          />
          <el-table-column
            prop="username"
            label="用户名"
            align="center"
            width="250"
          />
          <el-table-column
            prop="nick_name"
            label="昵称"
            align="center"
            width="180"
          />
          <el-table-column
            label="头像"
            width="180"
            align="center"
          >
            <template v-slot="scope">
              <img style="width:120px;height:80px" :src="scope.row.salt" alt="">
            </template>
          </el-table-column>

          <el-table-column
            prop="roles"
            label="角色"
            width="180"
            align="center"
          />

          <el-table-column
            prop="create_time"
            label="添加时间"
            align="center"
            width="200"
          />

          <el-table-column
            prop=""
            label="操作"
            fixed="right"
            align="center"
            width="150"
          >
            <template v-slot="scope">
              <el-button type="primary" size="small" @click="updateUser(scope.row.id)">编辑</el-button>
              <el-button type="primary" size="small" @click="delUser(scope.row.id)">删除</el-button>
            </template>

          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="start"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog
      title="编辑用户"
      :visible.sync="isShow"
      width="40%"
    >
      <el-form :model="form" :inline="true" label-width="80px" size="mini">
        <el-form-item label="用户名">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="用户昵称">
          <el-input v-model="form.nickName" />
        </el-form-item>
        <el-form-item v-if="isAdd" label="密码">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="用户头像">
              <el-upload
                class="avatar-uploader"
                action="http://leju.bufan.cloud/lejuAdmin/material/uploadFileOss"
                :headers="token"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
              >
                <img
                  v-if="form.salt"
                  style="width:40px;height: 40px;border-radius: 20px;"
                  :src="form.salt"
                  class="avatar"
                >
                <i v-else class="el-icon-s-custom avatar-uploader-icon imgbox" />
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="选择角色">
          <el-select v-model="form.roleIds" multiple placeholder="请选择">
            <el-option
              v-for="item in rolesList"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            />
          </el-select>

        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click=" isShow = false">取消</el-button>
        <el-button type="primary" @click="addUser">确定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import mixins from '@/mixins/index'
import { getToken } from '@/utils/myAuth'
import { findUsersByPage, saveUserRoles, findAllRoles, user, updateUserRoles, removeUser } from '@/api/rightsManage/account/index'
export default {
  name: 'VueAdminTemplateMasterIndex',
  mixins: [mixins],
  data() {
    return {
      token: {
        token: getToken()
      },
      isShow: false,
      userInfo: {
        'nickName': '',
        'username': ''
      },
      userList: [],
      form: {
        'createTime': '',
        'id': '',
        'isDeleted': false,
        'modifyTime': '',
        'nickName': '',
        'password': '',
        'roleIds': [],
        'salt': '',
        'username': ''
      },
      rolesList: [],
      isAdd: ''
    }
  },

  created() {
    this.init()
  },

  mounted() {

  },

  methods: {
    delUser(id) {
      this.$confirm('确定删除用户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        removeUser(id).then(res => {
          this.init()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    updateUser(id) {
      this.isAdd = false
      user(id).then(res => {
        console.log(res)
        this.form = res.data.user
        this.isShow = true
      })
    },
    addUser() {
      this.isShow = false
      if (this.isAdd) {
        saveUserRoles(this.form).then(res => {
          this.init()
        })
      } else {
        updateUserRoles(this.form).then(res => {
          this.init()
        })
      }
    },
    handleAvatarSuccess(e) {
      console.log(e)
      this.form.salt = e.data.fileUrl
    },
    init() {
      findAllRoles().then(res => {
        this.rolesList = res.data.items
      })
      findUsersByPage(this.start, this.limit).then(res => {
        console.log(res)
        this.total = res.data.total
        this.userList = res.data.rows
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .imgbox{
    width:40px;
    height: 40px;
    border-radius: 20px;
    background-color: #c0c4cc;
    color: #fff;
    line-height: 40px;
    font-size: 18px;
  }
</style>
